package com.example.rabbitmqboot.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cxu
 * @version 1.0.0
 * @description RabbitMQ配置类
 * @date 2021年09月21日 17:20:00
 */
@Configuration
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitConfig {
    private String host;
    private Integer port;
    private String username;
    private String password;
    private String virtualHost;

    /**
     * @description 创建rabbit连接工厂
     * @author cxu
     * @date 2021/9/23 8:30
     * @return: org.springframework.amqp.rabbit.connection.ConnectionFactory
     */
//    @Bean
//    public ConnectionFactory connectionFactory() {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setAddresses(host);
//        connectionFactory.setPort(port);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setVirtualHost(virtualHost);
//        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
//        connectionFactory.setPublisherReturns(true);
//        return connectionFactory;
//    }

    @Bean
    public Exchange topicExchange() {
        return ExchangeBuilder.topicExchange("topic_exchange").build();
    }

    @Bean
    public Queue topicQueue() {
        return QueueBuilder.nonDurable("topic_queue").build();
    }

    @Bean
    public Binding topicBinding(@Qualifier("topicQueue") Queue topicQueue, @Qualifier("topicExchange") Exchange topicExchange) {
        return BindingBuilder.bind(topicQueue).to(topicExchange).with("topic.#").noargs();
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVirtualHost() {
        return virtualHost;
    }

    public void setVirtualHost(String virtualHost) {
        this.virtualHost = virtualHost;
    }
}
